//
//  searchresultViewController.m
//  UI_xiangmu
//
//  Created by dllo on 15/4/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "searchresultViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "searchresultmodel.h"
#import "ClassifyotherCell.h"
#import "detailCamereViewController.h"
#import "MJRefresh.h"
@interface searchresultViewController ()

@property(nonatomic,retain)NSMutableArray *modelArr;

@property(nonatomic,retain)UITableView *mytableview;

@property(nonatomic,assign)NSInteger page;

@end

@implementation searchresultViewController

-(void)data
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=search&word=%@&page=1&limit=20&type=0",self.canshu];
     NSString *str1 =[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manager GET:str1 parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.modelArr = [NSMutableArray array];
        NSMutableArray *arr = [responseObject objectForKey:@"list"];
        for (NSMutableDictionary *dic in arr) {
            searchresultmodel *model = [[searchresultmodel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.modelArr addObject:model];
            [model release];
        }
        [self.mytableview reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"解析错误");
    }];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    detailCamereViewController *detile = [[detailCamereViewController alloc] init];
    detile.canshu = [self.modelArr[indexPath.row] id];
    detile.canshu = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=getInfo&video_id=%@&uid=(null)",detile.canshu];
    [self.navigationController pushViewController:detile animated:YES];
    [detile release];
}

- (void)method
{
    [self.mytableview addLegendHeaderWithRefreshingBlock:^{
        [self.modelArr removeAllObjects];
        self.page = 0;
        [self data];
        [self.mytableview reloadData];
        [self.mytableview.header endRefreshing];
    }];
    [self.mytableview.legendHeader beginRefreshing];
    [self.mytableview addLegendFooterWithRefreshingBlock:^{
        self.page++;
        [self data];
        [self.mytableview reloadData];
        [self.mytableview.footer endRefreshing];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.mytableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStylePlain];
    self.mytableview.delegate = self;
    self.mytableview.dataSource = self;
    [self.view addSubview:self.mytableview];
    [_mytableview release];
    [self data];
    self.mytableview.rowHeight = 120;
    [self method];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    ClassifyotherCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[ClassifyotherCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    NSURL *url = [NSURL URLWithString:[self.modelArr[indexPath.row] cover]];
    [cell.myimage sd_setImageWithURL:url];
    cell.namelabel.text = [self.modelArr[indexPath.row] name];
    searchresultmodel *model = self.modelArr[indexPath.row];
    if ([[self.modelArr[indexPath.row] isOver] isEqual:@"1"]) {
      
   NSString *str1 = [NSString stringWithFormat:@"全%@集",model.update];
        cell.jishulabel.text =str1;
        
    }else{
        NSString *str1 = [NSString stringWithFormat:@"更新至%@集",model.update];
        cell.jishulabel.text =str1;
        
    }
    NSString *str = [NSString stringWithFormat:@"%@分",[self.modelArr[indexPath.row]score]];
    cell.scorelabel.text = str;
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
